package com.zzw.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.bean.Supplier;
import com.zzw.springboot.bean.SupplierCooperation;
import com.zzw.springboot.service.SupplierCooperationService;
import com.zzw.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier/cooperation")
public class SupplierCooperationController {

    @Autowired
    private SupplierCooperationService cooperationService;

    /**
     * 获取合作关系列表
     */
    @GetMapping("/list")
    public Result listCooperations(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            SupplierCooperation cooperation) {

        //创建lambdaQueryWrapper, 封装查询条件
        LambdaQueryWrapper<SupplierCooperation> lambdaQueryWrapper = Wrappers.<SupplierCooperation>lambdaQuery();
        Page<SupplierCooperation> page = cooperationService.page(new Page<SupplierCooperation>(pageNum, pageSize), lambdaQueryWrapper);
        return Result.success(page);
    }

    /**
     * 根据供应商ID获取合作关系
     */
    @GetMapping("/supplier/{supplierId}")
    public Result getCooperationsBySupplierId(@PathVariable("supplierId") Integer supplierId) {
        List<SupplierCooperation> list = cooperationService.getCooperationsBySupplier(supplierId);
        return Result.success().add("data", list);
    }

    /**
     * 获取合作关系详情
     */
    @GetMapping("/{cooperationId}")
    public Result getCooperationById(@PathVariable("cooperationId") Integer cooperationId) {
        SupplierCooperation cooperation = cooperationService.getCooperationById(cooperationId);
        return Result.success().add("data", cooperation);
    }

    /**
     * 新增合作关系
     */
    @PostMapping
    public Result addCooperation(@Valid @RequestBody SupplierCooperation cooperation, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Result.error().add("errorFields", map);
        }

        cooperationService.addCooperation(cooperation);
        return Result.success();
    }

    /**
     * 修改合作关系
     */
    @PutMapping
    public Result updateCooperation(@Valid @RequestBody SupplierCooperation cooperation, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Result.error().add("errorFields", map);
        }

        cooperationService.updateCooperation(cooperation);
        return Result.success();
    }

    /**
     * 删除合作关系
     */
    @DeleteMapping("/{cooperationId}")
    public Result deleteCooperation(@PathVariable("cooperationId") Integer cooperationId) {
        cooperationService.deleteCooperation(cooperationId);
        return Result.success();
    }
}
